package com.zhu.ttwords.view.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhu.ttwords.R;
import com.zhu.ttwords.TTWORDS;
import com.zhu.ttwords.presenter.StudyPresenter;
import com.zhu.ttwords.view.adapter.StudyAdapter;
import com.zhu.ttwords.view.listener.StudyListener;

import java.util.List;
import java.util.Locale;

public class StudyActivity extends ActionBarActivity implements IStudyActivity {


    StudyPresenter presenter;
    ImageView mao;
    ViewPager viewPager;
    StudyAdapter adapter;
    StudyListener listener;
    TextView index_total;
    TextView showMode;

    Dialog examComplateDialog;
    Dialog exitDialog;
    Dialog studyComplateDialog;
    int exam_total = 1;
    int exam_right = 0;
    String exam_percent = "0.00%";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_study);
        // 初始化UI
        index_total = (TextView) this.findViewById(R.id.activity_study_index_total);
        showMode = (TextView) this.findViewById(R.id.activity_study_showmode);
        mao = (ImageView) this.findViewById(R.id.activity_study_mao);
        viewPager = (ViewPager) this.findViewById(R.id.activity_study_viewpager);
        presenter = new StudyPresenter(this);
        presenter.initStudy();
        listener = new StudyListener(presenter);
        viewPager.setOnPageChangeListener(listener);
        viewPager.setOffscreenPageLimit(TTWORDS.getAppSetting().getGroup());

        studyComplateDialog = new AlertDialog.Builder(this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("进行测试才能保留学习成果哦，亲！")
                .setItems(new String[]{"● 开始测试", "● 结束学习", "● 再看一会"},
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                switch (which) {
                                    case 0:
                                        presenter.initExam();
                                        break;
                                    case 1:
                                        StudyActivity.this.finish();
                                        break;
                                    case 2:
                                        dialog.dismiss();
                                        break;
                                }
                            }
                        }).create();

        examComplateDialog = new AlertDialog.Builder(
                StudyActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("没复习完就不能学习新词哦，么么哒！")
                .setItems(
                        new String[]{
                                "正确数： " + exam_right + "\n错误数： "
                                        + (exam_total - exam_right)
                                        + "\n正确率： " + exam_percent
                                        + "%",
                                "● 新的测试",
                                "● 学习模式",
                                "● 结束测试"},
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(
                                    DialogInterface dialog,
                                    int which) {
                                switch (which) {
                                    case 0:
                                        examComplateDialog.dismiss();
                                        break;
                                    case 1:
                                        presenter.initExam();
                                        break;
                                    case 2:
                                        presenter.initStudy();
                                        break;
                                    case 3:
                                        StudyActivity.this.finish();
                                        break;
                                }
                            }
                        }).create();

        exitDialog = new AlertDialog.Builder(this).setTitle("退出本次学习")
                .setMessage("完成学习了吗？")
                .setPositiveButton("退出", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        StudyActivity.this.finish();
                    }
                }).setNegativeButton("继续学习", null).create();

        mao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.showMenu();
            }
        });
        selectInputMethod();
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                exitDialog.show();
                break;
        }
        return true;
    }

    @Override
    public void initStudyData(List<Object> mData) {
        adapter = new StudyAdapter(this, mData);
        viewPager.setAdapter(adapter);
        index_total.setText("1/" + adapter.getCount());

    }

    @Override
    public void initExamData(List<Object> mData) {
        adapter = new StudyAdapter(this, mData);
        viewPager.setAdapter(adapter);
        index_total.setText("1/" + adapter.getCount());
    }

    @Override
    public void updatePageIndex(int index) {
        index_total.setText(index + "/" + adapter.getCount());
    }

    @Override
    public void showMenu(int mode) {
        switch (mode){
            case MODE_STUDY:
                studyComplateDialog.show();
                break;
            case MODE_EXAM:
                examComplateDialog.show();
                break;
            case MODE_REVIEW:
                break;
        }
    }

    private void selectInputMethod(){
        InputMethodManager immanager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        List<InputMethodInfo> list = immanager.getEnabledInputMethodList();
        String defaultIme = Settings.Secure
                .getString(this.getContentResolver(),
                        Settings.Secure.DEFAULT_INPUT_METHOD);
        InputMethodInfo default_info = null;
        boolean hasJPInpute = false;
        for (InputMethodInfo inputMethodInfo : list) {
            if (inputMethodInfo.getId().equals(defaultIme)) {
                default_info = inputMethodInfo;
            }

            if (!(inputMethodInfo.getSubtypeCount() > 0 && inputMethodInfo
                    .getSubtypeAt(0).getLocale()
                    .equals(Locale.JAPAN.toString()))) {
                hasJPInpute = true;
            }
        }
        if (default_info != null && hasJPInpute) {
            immanager.showInputMethodPicker();
        } else {
            Toast.makeText(this, "请安装日语输入法", Toast.LENGTH_SHORT);
        }
    }
}
